package itsos.controller;


import itsos.model.*;
import itsos.service.*;

import java.sql.Timestamp;
import java.util.List;

import org.springframework.web.servlet.mvc.multiaction.MultiActionController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import itsos.common.*;



public class MainIssueController extends MultiActionController {
	
	private MainIssueService mainIssueService;
	private RepositoryService repositoryService;
	private IssueService issueService;
	private IssueBoxService issueBoxService;
	private MemberService memberService;
	

	
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	public void setIssueBoxService(IssueBoxService issueBoxService) {
		this.issueBoxService = issueBoxService;
	}	
	
	public void setIssueService(IssueService issueService)
	{
		this.issueService = issueService;
	}
	
	public void setRepositoryService(RepositoryService repositoryService)
	{
		this.repositoryService = repositoryService;
	}
	
	
	public void setMainIssueService(MainIssueService mainIssueService)
	{
		this.mainIssueService = mainIssueService;
	}
	
	public ModelAndView viewMainIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/mainView");
		List list = issueService.IssueList(request);
		
		MainIssue mainIssue2 = mainIssueService.getMainIssue(request);
		if(mainIssue2.getAdminID()!=999)
		{	
		
		MainIssue mainIssue = mainIssueService.viewMainIssue(request);
		
		mav.addObject("issueList", list);
		
		mav.addObject("viewMainIssue", mainIssue);
		}
		else
		{
			mainIssue2.setMemberName(ConstCode.SUPER_ADMIN_ID);
			
			mav.addObject("issueList", list);
			mav.addObject("viewMainIssue", mainIssue2);
		}
		
		mav.addObject("repositoryID", request.getParameter("repositoryID"));
		
		
		if(request.getSession().getAttribute("memberID")!=null)
		mav.addObject("issueBox", issueBoxService.getIssueBox(request));
		mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
		
	
	public ModelAndView doMainIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/procMainIssue");
		
		mav.addObject("viewMainIssue", mainIssueService.getAdminMainIssue(request));
		mav.addObject("regIssue", issueService.getRegIssue(request));
		mav.addObject("devMember", memberService.devMember());
		
		mav.addObject("repositoryID", request.getParameter("repositoryID"));
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView updateMainIssue(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/mainView");
		if((request.getParameter("adminContent")!=null) && (request.getParameter("releaseInfo")!=null))
		{
			mainIssueService.adminUpdate(request);
		}
		if((request.getParameter("regIssue")!=null) && (request.getParameter("devMember")!=null) && request.getParameter("processorContent")!=null)
		{
			issueService.updateAdmin(request);
		}
		
		List list = issueService.IssueList(request);
		
		MainIssue mainIssue2 = mainIssueService.getMainIssue(request);
		if(mainIssue2.getAdminID()!=999)
		{	
		
		MainIssue mainIssue = mainIssueService.viewMainIssue(request);
		
		mav.addObject("issueList", list);
		
		mav.addObject("viewMainIssue", mainIssue);
		}
		else
		{
			mainIssue2.setMemberName(ConstCode.SUPER_ADMIN_ID);
			
			mav.addObject("issueList", list);
			mav.addObject("viewMainIssue", mainIssue2);
		}
		
		mav.addObject("repositoryID", request.getParameter("repositoryID"));
		
				
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	public ModelAndView adminProc(HttpServletRequest request, HttpServletResponse response)
	{
		ModelAndView mav = new ModelAndView("workpage/mainissue/adminProc");
		mav.addObject("adminProcIssue",issueService.adminProc(request));
		
		
		
		if(request.getSession().getAttribute("memberID")!=null)
			mav.addObject("issueBox", issueBoxService.getIssueBox(request));
			mav.addObject("treeView", repositoryService.getRepository());
		return mav;
	}
	
	
	
}
